#include<iostream>
using namespace std;

void Gauss_with_column_element(double *A,double *b,int n);

void Gauss_with_column_element(double *A,double *b,int n)
{
    int i,j,k,col;
    double x[n],a,s,temp,max;

    for(i=0;i<n;i++)
    x[i]=0;
    //消去过程
	  for(k=0;k<n;k++)
	  {
	  	max=fabs(*(A+k*n+k));
	  	col=k;
	  	for(i=k;i<n;i++)
	  	{
	  		if(max<fabs(*(A+i*n+k)))
	  		{
	  			max=fabs(*(A+i*n+k));
	  			col=i;
	  		}
	  	}
	  	for(j=k;j<n;j++)
	  	{
	  		temp=*(A+col*n+j);
	  		*(A+col*n+j)=*(A+k*n+j);
	  		*(A+k*n+j)=temp;
	  	}
	  	temp=b[col];
	  	b[col]=b[k];
	  	b[k]=temp;
	  	if(!(*(A+k*n+k)))
	  	return -1;
	  	for(i=k+1;i<n;i++)
	  	{
	  		a=(*(A+i*n+k))/(*(A+k*n+k));
	  		for(j=k;j<n;j++)
	  		{
	  			*(A+i*n+j)=*(A+i*n+j)-a*(*(A+k*n+j));
	  		}
	  		b[i]=b[i]-a*b[k];
	  	}
	  } 
	  //消去结果
	 	 cout<<"U="<<endl;
	 for(i=0;i<n;i++)
	 {
	   for(j=0;j<n;j++)
	     cout<<*(A+i*n+j)<<" ";
	 cout<<endl;
	 }
	 //回代求解
	  	 x[n-1]=b[n-1]/(*(A+(n-1)*n+n-1));
	 for(k=n-2;k>=0;k--)
	 {
	 	s=b[k];
	 	for(j=k+1;j<n;j++)
	 	{
	 		s=s-(*(A+k*n+j))*x[j];
	 	}
	 	x[k]=s/(*(A+k*n+k));
	 } 
	 //result
	 	 cout<<"x="<<endl;
	 for(i=0;i<n;i++)
	 cout<<x[i]<<"  ";
	 cout<<endl; 
}